﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument.Document
{
    /// <summary>
    /// ListView 的摘要说明。
    /// </summary>
    public partial class ListViewPage : Page
    {
        protected string SortBy = "DocAddedDate";
        protected string SortRule = "Desc";
        protected string Username;
        protected string ClassID;
        protected string DisplayType = "";

        protected override void OnInit(EventArgs e)
        {
            lnkbtnDelete.Click += lnkbtnDelete_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            ClassID = (Request.QueryString["ClassID"] != null) ? Request.QueryString["ClassID"] : "";
            DisplayType = (Request.QueryString["DisplayType"] != null) ? Request.QueryString["DisplayType"] : "0";
            Username = SiteContext.Current.UserName;
            lnkbtnDelete.Attributes["onclick"] = "javascript:return confirm('您确认要丢弃吗?');";
            if (!IsPostBack)
            {
                BindGrid();
                ShowAvailable();
            }

            base.OnLoad(e);
        }

        private void lnkbtnDelete_Click(object sender, EventArgs e)
        {
            var doc = new DocumentClass();
            bool sqlFlag = true;
            string sql = "";
            foreach (DataGridItem dgi in dgDocList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    int i = dgi.ItemIndex;
                    string id = dgDocList.DataKeys[i].ToString();
                    if (sqlFlag)
                    {
                        sql += id;
                        sqlFlag = false;
                    }
                    else
                    {
                        sql += " ,";
                        sql += id;
                    }
                }
            }
            //选择为空
            if (sql == String.Empty)
            {
                Response.Write("<script language=javascript>alert('请选择文档!');window.location='ListView.aspx?ClassID=" +
                               ClassID + "';</script>");
            }
            else
            {
                int deleteFlag = DisplayType == "3" ? 1 : 0;
                if (doc.DocDelete(sql, deleteFlag))
                {
                    Response.Write(
                        "<script language=javascript>alert('文档丢弃成功!');window.location='ListView.aspx?ClassID=" + ClassID +
                        "';</script>");
                }
                else
                {
                    Server.Transfer("../../Error.aspx");
                }
            }
        }

        #region 数据绑定至DataGrid

        /// <summary>
        /// 将某用户的邮件取出绑定至DataGrid
        /// </summary>
        protected void BindGrid()
        {
            var doc = new DocumentClass();
            SqlDataReader dr = doc.GetDocListInClass(Int32.Parse(ClassID), Username, Int32.Parse(DisplayType));
            DataTable datatable = Tools.ConvertDataReaderToDataTable(dr);
            DataView source = datatable.DefaultView;
            source.Sort = SortBy + " " + SortRule;
            dgDocList.DataSource = source;
            dgDocList.DataBind();
            //		
            //			if (datatable.Rows.Count !=0)
            //			{
            //				this.lnkbtnDelete.Visible =true;
            //			
            //			}
        }

        #endregion

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgDocList.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
            //	ShowAvailable();
        }

        #endregion

        #region 将数据排序

        /// <summary>
        /// 将信件按照指定字段进行排序
        /// </summary>
        protected void DataGrid_Sort(Object Src, DataGridSortCommandEventArgs E)
        {
            SortRule = (SortRule == "Desc") ? "Asc" : "Desc";
            SortBy = E.SortExpression;
            var doc = new DocumentClass();
            DataTable datatable;
            using (var dr = doc.GetDocListInClass(Int32.Parse(ClassID), Username, Int32.Parse(DisplayType)))
            {
                datatable = Tools.ConvertDataReaderToDataTable(dr);
            }
            DataView Source = datatable.DefaultView;
            Source.Sort = SortBy + " " + SortRule;
            dgDocList.DataSource = Source;
            dgDocList.DataBind();
        }

        #endregion

        #region 根据权限显示相关功能标题

        // 设置缺省值
        public void ShowAvailable()
        {
            string userName = SiteContext.Current.UserName;
            int classID = Int32.Parse(ClassID);

            lblManageDirectory.Visible = AssignRights.IsAccessAllowed(userName, classID, PermissionType.ManageNodes);
            lblDeliveryDoc.Visible = AssignRights.IsAccessAllowed(userName, classID, PermissionType.DeliveryDocument);
            lblShowMember.Visible = AssignRights.IsAccessAllowed(userName, classID, PermissionType.ManageTeam);
            lblManagePermission.Visible = AssignRights.IsAccessAllowed(userName, classID, PermissionType.ManagePermission);
            lnkbtnDelete.Visible = AssignRights.IsAccessAllowed(userName, classID, PermissionType.DeleteDocument);
            lblRemove.Visible = AssignRights.IsAccessAllowed(userName, classID, PermissionType.MoveProject);
            lblCopy.Visible = AssignRights.IsAccessAllowed(userName, classID, PermissionType.MoveProject);
        }

        #endregion
    }
}